// 引入redux 的依赖
// combine联合，合并
// applyMiddleware 使用中间件
import {createStore, combineReducers, applyMiddleware} from 'redux'
import {CHANGEAGE, CHANGENAME} from './type'
// 引入依赖,将数据通过devtool展示
import {composeWithDevTools} from 'redux-devtools-extension'
// 导入不同模块中的内容
import orderReducer from './modules/order'
import userReducer from './modules/user'
// 导入日志中间件
import logger from 'redux-logger'

// 初始化仓库中的数据
const initState = {
  name: 'Tom',
  age: 18
}

// 修改数据的纯函数：reducer
const reducer = (state=initState, action)=>{
  // action就是一个js的obj，一定会有一个属性叫type：{type: xxx, name: 'Jack'}
  switch (action.type) {
    case CHANGENAME:
      // 返回一个对象，作为新的state
      return {
        ...state,
        name: action.name
      }
    case CHANGEAGE:
      return {
        ...state,
        age: action.age
      }
  
    default:
      return state
  }
}

// 通过createStore方法创建仓库，参数就是reducer
// const store = createStore(reducer);


// 通过combineReducers方法，合并两个模块中的reducer
let rootReducer = combineReducers({
  order: orderReducer,
  user: userReducer,
})
// const store = createStore(rootReducer);
// 自己封装一个logger中间件
// const logger = store => next => action => {
//   console.log('触发的是：', action);
//   let result = next(action);
//   console.log('数据：',store.getState());
//   console.log('继续执行：',result);
//   return result
// }
const store = createStore(rootReducer, composeWithDevTools(applyMiddleware(logger)))

export default store